<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-11-6
 * Time: 下午2:35
 */

namespace console\modules\gps\controllers;


use common\components\tools\Api;
use common\models\CarGpsOrder;
use common\models\credit_app\OrderCar;
use common\models\credit_app\OrderOldinfo;
use common\models\credit_app\Task;
use yii\console\Controller;
use yii\httpclient\Client;

class TaskController extends Controller
{
    /**
     * SELECT o_app_id, o_gps_no FROM dyhl_order_oldinfo
    WHERE o_gps_status IS NULL
    AND o_app_status = '终审通过'
    AND o_ad_money IS NULL
    AND (
    (o_gps_no LIKE '391%' AND LENGTH(o_gps_no) = 11)
    OR 	(o_gps_no LIKE '802%' AND LENGTH(o_gps_no) = 11)
    OR 	(o_gps_no LIKE '868%' AND LENGTH(o_gps_no) = 15)
    )
     */
    public function actionCheckOrderCar()
    {
        $oldinfos = OrderOldinfo::find()->select(['o_app_id', 'o_gps_no', 'o_first_time'])->where([
            'and',
            ['=', 'o_app_status', OrderOldinfo::O_APP_STATUS_PASS10],
            ['is', 'o_ad_money', null],
            [
                'or',
                ['and', ['like', 'o_gps_no', '391%', false], ['=', 'LENGTH(o_gps_no)', 11]],
                ['and', ['like', 'o_gps_no', '802%', false], ['=', 'LENGTH(o_gps_no)', 11]],
                ['and', ['like', 'o_gps_no', '868%', false], ['=', 'LENGTH(o_gps_no)', 15]],
            ],
        ])->asArray()->all();
        foreach($oldinfos as $k => $v){
            $data = [
                'gps_serial_number'=>$v['o_gps_no'],
                'is_direct'=>1,
            ];
            if ($v['o_first_time']){
                $data['other_data'] = serialize(['o_add_time'=>strtotime($v['o_first_time'])]);
            }
            $r = Api::request('/gps/api/get-gps', $data);
            switch ($r->data['data']['other_data']['gps_status']){
                case '已上线':
                    $order_car = OrderCar::findOne(['ot_gps_no'=>$v['o_gps_no']]);
                    $order_car->ot_gps_status = '已上线';
                    $order_car->ot_gps_otime = date("Y-m-d", $r->data['data']['other_data']['gps_online_time']);
                    $order_car->save();
                    var_dump('已上线');
                    $task = new Task();
                    $task->task_type = $task::TASK_TYPE_ORDER_CAR;
                    $task->task_releid = $order_car->ot_oid;
                    $task->task_status = $task::TASK_STATUS_NO;
                    $task->task_addtime = time();
                    $task->save();
                    var_dump('task add');
                    break;
                case '未上线':
                    if (isset($r->data['data']['other_data']['gps_online_time'])){
                        $order_car = OrderCar::findOne(['ot_gps_no'=>$v['o_gps_no']]);
                        $order_car->ot_gps_otime = date("Y-m-d", $r->data['data']['other_data']['gps_online_time']);
                        $order_car->save();
                        var_dump('未上线 修改ot_gps_otime');
                    }else{
                        var_dump('未上线');
                    }
                    break;
            }
        }
    }

    /**
     * php yii gps/task/beidou-name-add
     */
    public function actionBeidouNameAdd()
    {
        $orders = CarGpsOrder::find()->where(['or', ['like', 'gps_serial_number', '141%', false], ['like', 'gps_serial_number', '863%', false]])->asArray()->all();
        foreach ($orders as $k => $v) {
            $v['guarantee_company_sign_code'] = 'yishang';
            $resp = Api::request('/gps/api/add-gps', $v);
            var_dump($resp->content);
        }
    }
}